package main

import (
	"Logger/logAgent/config"
	"Logger/logAgent/etcd"
	"Logger/logAgent/kafka"
	"Logger/logAgent/tailfile"
	"fmt"
	"github.com/sirupsen/logrus"
	"gopkg.in/ini.v1"
)

//日志收集的客户端
//收集指定目录下的日志文件发送到kafka中

func main() {
	//1.读配置文件
	cfg, err := ini.Load("./logAgent/config/config.ini")
	if err != nil {
		logrus.Error("load config file,err:\n", err)
		return
	}
	//kafka_addr := cfg.Section("kafka").Key("address").String()
	var conf config.Config
	err = cfg.MapTo(&conf)
	if err != nil {
		logrus.Errorln("load config error: ", err)
		return
	}
	logrus.Printf("Ini Config: %#v", conf)
	//2.初始化kafka生产者连接
	err = kafka.InitSyncProducer([]string{conf.Address}, int32(conf.MsgSize))
	if err != nil {
		logrus.Fatalln(err)
	}
	logrus.WithFields(logrus.Fields{"kafka": "success"}).Info("kafka connect success")

	//3.初始化etcd连接
	err = etcd.Init([]string{conf.EtcdAddr})
	if err != nil {
		logrus.Errorf("Etcd init failed with error:%v\n", err)
	}

	//4.获取日志收集配置
	collections, err := etcd.GetCollections(conf.Key)
	if err != nil {
		logrus.Errorf("Etcd get config failed with error:%v\n", err)
	}
	fmt.Printf("collections: %v\n", collections)
	//5.初始化tail连接
	err = tailfile.Init(collections)
	if err != nil {
		logrus.Fatalln(err)
	}
	logrus.Info("Tail init success")
	//6.watch 监听配置变化
	tailfile.Watch(conf.Key)
	//3.把日志通过sarama发送到kafka
	//tailfile.Run(conf.Topic)
	select {}
}

////var wg sync.WaitGroup
////num := 100
////cat := make(chan struct{})
////dog := make(chan struct{})
////fish := make(chan struct{})
////wg.Add(3)
////go func(num int) {
////	for i := 0; i < num; i++ {
////		<-cat
////		fmt.Println(i, ":cat")
////		dog <- struct{}{}
////	}
////	wg.Done()
////}(num)
////go func(num int) {
////	for i := 0; i < num; i++ {
////		<-dog
////		fmt.Println(i, ":dog")
////		fish <- struct{}{}
////	}
////	wg.Done()
////}(num)
////go func(num int) {
////	for i := 0; i < num; i++ {
////		<-fish
////		fmt.Println(i, ":fish")
////		if i < num-1 {
////			cat <- struct{}{}
////		}
////	}
////	wg.Done()
////}(num)
////cat <- struct{}{}
////wg.Wait()
//var wg sync.WaitGroup
//num := 100
//cat := make(chan struct{})
//dog := make(chan struct{})
//fish := make(chan struct{})
//wg.Add(3)
//go func(num int) {
//	<-cat
//	for i := 0; i < num; i++ {
//
//		fmt.Println(i, ":cat")
//
//	}
//	dog <- struct{}{}
//	wg.Done()
//}(num)
//go func(num int) {
//	<-dog
//	for i := 0; i < num; i++ {
//
//		fmt.Println(i, ":dog")
//
//	}
//	fish <- struct{}{}
//	wg.Done()
//}(num)
//go func(num int) {
//	<-fish
//	for i := 0; i < num; i++ {
//
//		fmt.Println(i, ":fish")
//		//if i < num-1 {
//		//	cat <- struct{}{}
//		//}
//	}
//	wg.Done()
//}(num)
//cat <- struct{}{}
//wg.Wait()
